﻿Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbDataReader
Imports System.Data
Public Class signupbc
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            successcreatebookclub.Visible = False
        Else
            successcreatebookclub.Visible = True
        End If
        If User.Identity.IsAuthenticated Then
            PrimaryContact.Text = User.Identity.Name
        Else
            Response.Redirect("~/Default.aspx")
        End If
        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim cmd As New OleDbCommand("SELECT TOP 1 * FROM [FamousQuotes] ORDER BY rnd(INT(NOW*id)-NOW*id)", conn)
        conn.Open()
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        dr.Read()
        QuoteOfTheDay.Text = dr("Quote").ToString
        Author.Text = dr("SourceTitle").ToString
        dr.Close()
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim SqlString As String = "Insert into bookclubs(BookClubName,PrimaryContact,BookClubLocation) Values (@f1,@f2,@f3)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_bookclubname.Text)
        cmd.Parameters.AddWithValue("@f2", PrimaryContact.Text)
        cmd.Parameters.AddWithValue("@f3", dd_bookclublocation.SelectedValue)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        addRole()
    End Sub
    Sub addRole()
        Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim cmdselect As OleDbCommand = New OleDbCommand()
        cmdselect.Connection = con
        con.Open()
        Session("currentuser") = User.Identity.Name
        cmdselect.Parameters.AddWithValue("@currentUser", Session("currentuser"))
        cmdselect.CommandText = String.Format("SELECT * FROM aspnet_Users WHERE (UserName = ?)")
        Dim dr As OleDbDataReader
        dr = cmdselect.ExecuteReader()
        dr.Read()
        UserID.Text = dr("UserId").ToString
        dr.Close()
        con.Close()
        'Insert query after select query'
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim SqlString As String = "Insert into aspnet_UsersInRoles(UserId,RoleId) Values (@f1,@f2)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", UserID.Text)
        cmd.Parameters.AddWithValue("@f2", "1")
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        UserID.Text = Nothing
    End Sub
End Class